#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <glob.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

#define DELIMS " \t\n"

struct cmd_st
{
  glob_t globres;
};

static void prompt(void)
{
  printf("mysh$ ");
}

static void parse(char *line, struct cmd_st *cmd)
{
  char *tok;
  
  int i = 0;
  while (1) {
    tok = strsep(&line, DELIMS);
    if (tok == NULL)
      break;
    if (tok[0] == '\0') // empty str
      continue;
    
    glob(tok, GLOB_NOCHECK | GLOB_APPEND * i, NULL, &cmd->globres);
    i = 1;
  }
}

int main()
{
  char *linebuf = NULL;
  size_t linebuf_size = 0;
  struct cmd_st cmd;
  pid_t pid;

  while (1) {
    prompt(); // 打印提示符
    
    if (getline(&linebuf, &linebuf_size, stdin) < 0) {
      break;
    }

    parse(linebuf, &cmd); // 解析命令
    
    /* extern cmd */
    {
      pid = fork();
      if (pid < 0) {
        perror("fork");
	exit(1);
      }

      /* child process */
      if (pid == 0) {
        execvp(cmd.globres.gl_pathv[0], cmd.globres.gl_pathv);
        perror("exec");
        exit(1);	
      }

      wait(NULL);
    }
  }
  exit(0);
}
